package cp4.work;

import cp4.work.data.Goods;
import cp4.work.data.GoodsData;
import util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.List;

// 商品列表页
@WebServlet("/cp4/work/goods")
public class GoodsServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = StringUtil.getWriter(resp);
        Collection<Goods> goodsList = GoodsData.goodsList.values();//获取所有的值
        out.println("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "    <link rel=\"stylesheet\" href=\"/assets/css/bootstrap.min.css\">\n" +
                "</head>\n" +
                "<body>\n" +
                "    <div class=\"container\">\n" +
                "        <table class=\"table table-bordered\">\n" +
                "            <tr>\n" +
                "                <td>id</td>\n" +
                "                <td>标题</td>\n" +
                "                <td>价格</td>\n" +
                "                <td>操作</td>\n" +
                "            </tr>");
        for (Goods g : goodsList) {
            out.println("<tr>\n" +
                    "                <td>" + g.getId() + "</td>\n" +
                    "                <td>" + g.getTitle() + "</td>\n" +
                    "                <td>" + g.getPrice() + "</td>\n" +
                    "                <td><a href=\"/cp4/work/carts?type=add&id=" + g.getId() + "\">加入购物车</td>\n" +
                    "            </tr>");
        }
        out.println("</table>\n" +
                "    </div>\n" +
                "</body>\n" +
                "</html>");
    }
}
